package com.zxt.controller;

import com.zxt.pojo.Dept;
import com.zxt.pojo.Result;
import com.zxt.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RequestMapping("depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    //    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping//公共的可以抽取到@RequestMapping("depts") 然后就可以省略
    public Result getDeptList() {
        System.out.println("查询全部数据");
        List<Dept> deptList = deptService.getDeptList();
        return Result.success(deptList);
    }

    @DeleteMapping
    public Result deleteById(@RequestParam(value = "id", required = false) Integer id) {
        deptService.deleteById(id);
        System.out.println("Delete id=" + id);
        return Result.success();
    }
@PostMapping
    public Result addDept(@RequestBody Dept dept) {
        deptService.addDept(dept);
    System.out.println("Add dept:"+dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Dept dept=deptService.getById(id);
        System.out.println("Get information by id="+id);

        return Result.success(dept);

    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {

        deptService.updateDept(dept);

        System.out.println("Update dept:"+dept);
        return Result.success();
    }
}
